﻿using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Media;
namespace WPFExcelReport
{
    public partial class TLOGIN 
    {
        #region ==IsLogOn===========
        public static readonly DependencyProperty IsLogOnProperty = DependencyProperty.Register("IsLogOn",
            typeof(bool), typeof(TLOGIN),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsLogOnChanged));
        public bool IsLogOn
        {
            get
            {
                return (bool)GetValue(IsLogOnProperty);
            }
            set
            {
                SetValue(IsLogOnProperty, value);
                OnLogOnChg(value);
            }
        }
        private static void OnIsLogOnChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TLOGIN;
            if (src != null)
            {
                src.IsLogOn = (bool)e.NewValue;
            }
        }
        public virtual void OnLogOnChg(bool value)
        {
            if (value)
                return;
            if (app.User.Trim() == "ITXV")
                return;
            txtUserID.Text = "";
            txtPassword.Password = "";
        }
        #endregion 

     
        public TLOGIN()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return; 
            MV = new VICC_HRSTAMM();
            this.DataContext = MV;
            InitializeComponent(); 
            txtUserID.Text = app.User;
            txtPassword.Password = app.Pwd; 
        }
        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            if (IsLoaded)
                return;
            base.Window_Loaded(sender, e);
        }
         private void btnLogIn_Click(object sender, RoutedEventArgs e)
        {
            if(!app.Connected)
            {
                this.IsEnabled = false;
                return;
            }
            LogOn();
        }
        private void LogOn()
        {
            bool _IsLogOn = false;
            string Mandant = txtMANDANT.Text.ToUpper();
            string userID = Helpers.Helper.Left(txtUserID.Text.ToUpper() + "        ", 8);
            System.Security.SecureString SPassword = txtPassword.SecurePassword;
            IntPtr p = System.Runtime.InteropServices.Marshal.SecureStringToBSTR(SPassword);
            string Password = System.Runtime.InteropServices.Marshal.PtrToStringBSTR(p);
            string error = "";
            _IsLogOn = ((VICC_HRSTAMM)MV).UserValid(userID, Password,ref error);
            txtLogIn.Background = new SolidColorBrush(Colors.Green);
            if (_IsLogOn)
            {
                app.LogOn();
                app.User = userID.Trim().PadRight(8);
                this.KEY = "";
                this.KEY = userID;
            }
            IsLogOn = _IsLogOn;
        }
        private void txtPassword_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key != System.Windows.Input.Key.Enter)
                return;
            if (e.Key != System.Windows.Input.Key.Return)
                return;
            LogOn();
        }  
    }
}
